package me.zfz.business.controller.impl;

import me.zfz.base.bean.SmartResponse;
import me.zfz.base.filter.bean.FilterParam;
import me.zfz.base.tag.bean.*;
import me.zfz.business.bean.RequestPage;
import me.zfz.business.bean.SubmitDataBean;
import me.zfz.business.bean.UserInfo;
import me.zfz.business.bean.entity.TNRole;
import me.zfz.business.controller.base.BaseController;
import me.zfz.base.plugins.ZTreeData;
import me.zfz.business.service.MenuService;
import me.zfz.business.service.OPService;
import me.zfz.business.service.ResourceService;
import com.mixsmart.utils.StringUtils;
import me.zfz.business.constant.IWebConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 角色
 *
 * @author author
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    private static final String VIEW_DIR = IWebConstant.WEB_BASE_VIEW_DIR + "/role";

    @Autowired
    private OPService opServ;
    @Autowired
    private MenuService menuServ;
    @Autowired
    private ResourceService resServ;

    @RequestMapping("/list")
    public ModelAndView list(HttpSession session, ModelAndView modelView, RequestPage page) {
        Map<String, Object> params = null;
        UserInfo userInfo = getUserInfoFromSession(session);
        if (!isSuperAdmin(userInfo)) {
            params = new HashMap<String, Object>(1);
            params.put("roleIds", userInfo.getRoleIds().toArray());
        }
        SmartResponse<Object> smartResp = opServ.getDatas("role_mgr_list", params, page.getStartNum(), page.getPageSize());

        String uri = "role/list";
        addBtn = new EditBtn("add", "showPage/base_role_add", "添加角色", "900");
        editBtn = new EditBtn("edit", "showPage/base_role_edit", "role", "修改角色", "900");
        delBtn = new DelBtn("role/delete.json", "确定要删除选中的角色吗？", uri, null, null);
        refreshBtn = new RefreshBtn(uri, null, null);
        pageParam = new PageParam(uri, null, page.getPage(), page.getPageSize());

        alinks = new ArrayList<ALink>();
        ALink link = new ALink();
        link.setUri("role/show");
        link.setDialogTitle("已配置的权限");
        link.setDialogWidth("900");
        alinks.add(link);

        ModelMap modelMap = modelView.getModelMap();
        modelMap.put("smartResp", smartResp);
        modelMap.put("addBtn", addBtn);
        modelMap.put("editBtn", editBtn);
        modelMap.put("delBtn", delBtn);
        modelMap.put("refreshBtn", refreshBtn);
        modelMap.put("pageParam", pageParam);
        modelMap.put("alinks", alinks);

        modelView.setViewName(VIEW_DIR + "/list");
        return modelView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody
    SmartResponse<String> add(HttpSession session, TNRole role,
                              String[] menuId, SubmitDataBean submitBean) throws Exception {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (null != role) {
            role.setUserId(getUserInfoFromSession(session).getId());
            smartResp = roleServ.save(role, menuId, (null != submitBean ? submitBean.getTreeProps() : null));
        }
        return smartResp;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public @ResponseBody
    SmartResponse<String> edit(TNRole role, String[] menuId, SubmitDataBean submitBean) throws Exception {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (null != role) {
            smartResp = roleServ.update(role, menuId, (null != submitBean ? submitBean.getTreeProps() : null));
        }
        return smartResp;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public SmartResponse<String> delete(String id) {
        return roleServ.delete(id);
    }

    /**
     * 查看角色信息
     *
     * @param modelView
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/show")
    public ModelAndView show(ModelAndView modelView, String id) throws Exception {
        modelView.getModelMap().put("id", id);
        modelView.setViewName(VIEW_DIR + "/show");
        return modelView;
    }


    /**
     * 简单角色列表=》用户管理选择角色使用
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/simplist")
    @ResponseBody
    public SmartResponse<Object> simplist(HttpSession session, FilterParam searchParam,RequestPage page) throws Exception {
        UserInfo userInfo = getUserInfoFromSession(session);
        if (!isSuperAdmin(userInfo)) {
            if (null == searchParam) {
                searchParam = new FilterParam();
            }
            searchParam.setRoleIds(StringUtils.list2Array(userInfo.getRoleIds()));
        }
        return opServ.getDatas("role_simp_list", searchParam, page.getStartNum(), page.getPageSize());
    }

    /**
     * 查看该角色拥有的菜单
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/viewMenus")
    public @ResponseBody
    SmartResponse<ZTreeData> viewMenus(String id) throws Exception {
        SmartResponse<ZTreeData> smartResp = new SmartResponse<ZTreeData>();
        if (StringUtils.isNotEmpty(id)) {
            smartResp = menuServ.roleMenuTree(id);
        }
        return smartResp;
    }

    /**
     * 查看该角色拥有的资源
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/viewRes")
    public @ResponseBody
    SmartResponse<ZTreeData> viewRes(String id) throws Exception {
        SmartResponse<ZTreeData> smartResp = new SmartResponse<ZTreeData>();
        if (StringUtils.isNotEmpty(id)) {
            smartResp = resServ.selectedResAuthTree(id);
        }
        return smartResp;
    }

    /**
     * @param searchParam
     * @param modelView
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping("/addOrg")
    public ModelAndView addOrg(FilterParam searchParam, ModelAndView modelView, RequestPage page) throws Exception {
        String uri = "role/addOrg";
        SmartResponse<Object> smartResp = opServ.getDatas("role_addorg_list", searchParam, page.getStartNum(), page.getPageSize());
        pageParam = new PageParam(uri, ".bootstrap-dialog-message", page.getPage(), page.getPageSize());
        ModelMap modelMap = modelView.getModelMap();
        modelMap.put("smartResp", smartResp);
        modelMap.put("pageParam", pageParam);
        modelMap.put("searchParam", searchParam);
        modelView.setViewName(VIEW_DIR + "/addOrg");
        return modelView;
    }

    /**
     * @param submitDatas
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveOrg", method = RequestMethod.POST)
    public @ResponseBody
    SmartResponse<String> saveOrg(String submitDatas, String id) throws Exception {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (StringUtils.isNotEmpty(submitDatas) && StringUtils.isNotEmpty(id)) {
            String[] values = submitDatas.split(",");
//            smartResp = roleServ.addOrg2Role(id, values);
        }
        return smartResp;
    }

}
